import { defineStore } from 'pinia'
import { ref } from 'vue'
import ApiService from '../services/ApiService'

export const useRpaStore = defineStore('rpa', () => {
  const robots = ref([])
  const apps = ref([])
  const tasks = ref([])
  const loading = ref(false)
  const error = ref(null)

  // 获取所有机器人列表
  const fetchRobots = async () => {
    loading.value = true
    error.value = null
    try {
      robots.value = await ApiService.getRobots()
    } catch (err) {
      error.value = err.message
    } finally {
      loading.value = false
    }
  }

  // 获取所有应用列表
  const fetchApps = async () => {
    loading.value = true
    error.value = null
    try {
      apps.value = await ApiService.getApps()
    } catch (err) {
      error.value = err.message
    } finally {
      loading.value = false
    }
  }

  // 获取所有任务列表
  const fetchTasks = async () => {
    loading.value = true
    error.value = null
    try {
      tasks.value = await ApiService.getTasks()
    } catch (err) {
      error.value = err.message
    } finally {
      loading.value = false
    }
  }

  // 添加机器人
  const addRobot = (robotData) => {
    loading.value = true
    error.value = null

    return new Promise((resolve, reject) => {
      ApiService.callApi(
        '添加机器人',
        robotData,
        (result) => {
          if (result.success) {
            const newRobot = result.data
            robots.value.push(newRobot)
            loading.value = false
            resolve(newRobot)
          } else {
            error.value = result.message || '添加机器人失败'
            loading.value = false
            reject(error.value)
          }
        },
        (err) => {
          error.value = err.message || '添加机器人失败'
          loading.value = false
          reject(error.value)
        }
      )
    })
  }

  // 更新机器人
  const updateRobot = (robotData) => {
    loading.value = true
    error.value = null

    return new Promise((resolve, reject) => {
      ApiService.callApi(
        '更新机器人',
        robotData,
        (result) => {
          if (result.success) {
            const updatedRobot = result.data
            const index = robots.value.findIndex(r => r.id === updatedRobot.id)
            if (index !== -1) {
              robots.value[index] = { ...robots.value[index], ...updatedRobot }
            }
            loading.value = false
            resolve(updatedRobot)
          } else {
            error.value = result.message || '更新机器人失败'
            loading.value = false
            reject(error.value)
          }
        },
        (err) => {
          error.value = err.message || '更新机器人失败'
          loading.value = false
          reject(error.value)
        }
      )
    })
  }

  // 删除机器人
  const deleteRobot = (robotId) => {
    loading.value = true
    error.value = null

    return new Promise((resolve, reject) => {
      ApiService.callApi(
        '删除机器人',
        { id: robotId },
        (result) => {
          if (result.success) {
            robots.value = robots.value.filter(r => r.id !== robotId)
            loading.value = false
            resolve(true)
          } else {
            error.value = result.message || '删除机器人失败'
            loading.value = false
            reject(error.value)
          }
        },
        (err) => {
          error.value = err.message || '删除机器人失败'
          loading.value = false
          reject(error.value)
        }
      )
    })
  }

  // 获取机器人详情
  const getRobotDetails = (robotId) => {
    loading.value = true
    error.value = null

    return new Promise((resolve, reject) => {
      ApiService.callApi(
        '获取机器人详情',
        { id: robotId },
        (result) => {
          if (result.success) {
            loading.value = false
            resolve(result.data)
          } else {
            error.value = result.message || '获取机器人详情失败'
            loading.value = false
            reject(error.value)
          }
        },
        (err) => {
          error.value = err.message || '获取机器人详情失败'
          loading.value = false
          reject(error.value)
        }
      )
    })
  }

  return {
    robots,
    apps,
    tasks,
    loading,
    error,
    fetchRobots,
    fetchApps,
    fetchTasks,
    addRobot,
    updateRobot,
    deleteRobot,
    getRobotDetails
  }
})